"""
DeepSpeed MOE + Qwen3-Coder Web API系统配置文件
"""

import os
from typing import List, Dict, Any

# API服务配置
class APIConfig:
    """API服务器配置"""
    HOST: str = "0.0.0.0"
    PORT: int = 8000
    WORKERS: int = 4
    TIMEOUT: int = 300
    MAX_REQUEST_SIZE: int = 50 * 1024 * 1024  # 50MB
    
    # CORS配置
    ALLOWED_ORIGINS: List[str] = ["*"]
    ALLOWED_METHODS: List[str] = ["GET", "POST", "PUT", "DELETE", "OPTIONS"]
    ALLOWED_HEADERS: List[str] = ["*"]

# DeepSpeed配置
class DeepSpeedConfig:
    """DeepSpeed MOE推理配置"""
    # 模型路径 - 需要根据实际部署情况调整
    MODEL_PATH: str = "/workspace/models/qwen3-coder-480b-a35b-instruct"
    
    # 并行配置
    TENSOR_PARALLEL_SIZE: int = 8  # 张量并行度
    EXPERT_PARALLEL_SIZE: int = 8  # 专家并行度
    PIPELINE_PARALLEL_SIZE: int = 1  # 流水线并行度
    
    # MOE配置
    NUM_EXPERTS: int = 128  # 专家数量
    MOE_LAYER_TYPE: str = "residual"  # MoE层类型: standard/residual
    
    # 推理配置
    DTYPE: str = "fp16"  # 数据类型: fp16/bf16/fp32
    REPLACE_WITH_KERNEL_INJECT: bool = True  # 是否使用内核注入
    USE_CACHE: bool = True  # 是否使用KV缓存
    
    # 生成配置
    MAX_NEW_TOKENS: int = 4096
    TEMPERATURE: float = 0.1
    TOP_P: float = 0.95
    TOP_K: int = 50
    REPETITION_PENALTY: float = 1.1

# 模型配置
class ModelConfig:
    """模型配置"""
    MODEL_NAME: str = "qwen3-coder-480b-a35b-instruct"
    MODEL_TYPE: str = "qwen3-coder"
    
    # 上下文配置
    CONTEXT_LENGTH: int = 262144  # 256K tokens
    MAX_POSITION_EMBEDDINGS: int = 1048576  # 1M tokens with extrapolation
    
    # 分词器配置
    TOKENIZER_PATH: str = None  # 如果为None则使用模型路径
    
    # 系统提示
    SYSTEM_PROMPT: str = """你是一个专业的代码助手，擅长各种编程任务，包括：
- 代码生成和补全
- 代码审查和优化
- 调试和错误修复
- 架构设计和技术咨询
- 文档编写和注释

请始终提供清晰、准确、实用的代码解决方案。"""

# API Key配置（实际部署时应该从环境变量读取）
class AuthConfig:
    """认证配置"""
    API_KEYS: Dict[str, str] = {
        "roo-code": "roo-code-key-2024",
        "claude-code": "claude-code-key-2024",
        "local": "local-dev-key-2024"
    }
    
    # JWT配置（如果需要更复杂的认证）
    JWT_SECRET_KEY: str = "your-secret-key-change-in-production"
    JWT_ALGORITHM: str = "HS256"
    JWT_EXPIRE_MINUTES: int = 60

# 日志配置
class LoggingConfig:
    """日志配置"""
    LOG_LEVEL: str = "INFO"
    LOG_FORMAT: str = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    LOG_FILE: str = "/workspace/logs/api.log"
    LOG_MAX_SIZE: int = 100 * 1024 * 1024  # 100MB
    LOG_BACKUP_COUNT: int = 5

# 缓存配置
class CacheConfig:
    """缓存配置"""
    REDIS_URL: str = "redis://localhost:6379/0"
    CACHE_TTL: int = 3600  # 1小时
    CACHE_MAX_SIZE: int = 1000
    ENABLE_RESPONSE_CACHE: bool = True

# 监控配置
class MonitoringConfig:
    """监控配置"""
    ENABLE_METRICS: bool = True
    METRICS_PORT: int = 9090
    HEALTH_CHECK_ENDPOINT: str = "/health"
    PROMETHEUS_METRICS: bool = True

# 环境配置
ENVIRONMENT = os.getenv("ENVIRONMENT", "development")
DEBUG = os.getenv("DEBUG", "true").lower() == "true"

# 导出所有配置
__all__ = [
    "APIConfig",
    "DeepSpeedConfig", 
    "ModelConfig",
    "AuthConfig",
    "LoggingConfig",
    "CacheConfig",
    "MonitoringConfig",
    "ENVIRONMENT",
    "DEBUG"
]